<?php

namespace App\Models\Traits;

use Carbon\Carbon;
use Illuminate\Support\Str;
use Zhuzhichao\BankCardInfo\BankCard as BankCardInterface;
use Illuminate\Validation\ValidationException;


trait HasUserCart
{
	
	public function addCart()
	{

		$carts = $this->carts()->where('product_id',request()->product_id)->where('sku_id',request()->sku_id)->where('is_closed',false);
		if($carts->first()){
			$number = big_decimal($carts->first()->number)->plus(request()->number);
			$carts->update(['number'=> $number]);
			$cart = $carts->first();
		}else{
			$insertArr = [
				'product_id' => request()->product_id,
				'sku_id' => request()->sku_id,
				'number' => request()->number,
			];
			$cart = $this->carts()->create($insertArr);
		}
	
		return $cart;
	}
	
}

